<template>
  <div>
    <van-nav-bar
      title="注册"
      left-text="返回"
      left-arrow
      @click-left="goBack"
    />

    <van-form @submit="onSubmit">
      <!-- 用户名 -->
      <van-field
        v-model="name"
        name="name"
        label="用户名"
        placeholder="用户名"
        required
        :rules="[{ required: true, message: '请填写用户名' }]"
      />

      <!-- 密码 -->
      <van-field
        v-model="pwd"
        type="password"
        name="pwd"
        label="密码"
        placeholder="密码"
        required
        :rules="[{ required: true, message: '请填写密码' }]"
      />

      <!-- 电话 -->
      <van-field
        v-model="phone"
        required
        label="手机号"
        placeholder="请输入手机号"
        name="phone"
        :rules="[{ required: true, message: '请填写电话' }]"
      />

      <!-- 性别 -->
      <van-field name="sex" label="单选框">
        <template #input>
          <van-radio-group v-model="sex" direction="horizontal">
            <van-radio name="1">男</van-radio>
            <van-radio name="2">女</van-radio>
          </van-radio-group>
        </template>
      </van-field>

      <!-- 生日 -->
      <van-field
        readonly
        clickable
        name="brith"
        :value="birth"
        label="时间选择"
        placeholder="点击选择时间"
        @click="showPicker = true"
      />
      <van-popup v-model="showPicker" position="bottom">
        <van-datetime-picker
          type="date"
          @confirm="onConfirm"
          @cancel="showPicker = false"
        />
      </van-popup>

      <!-- 邮箱 -->
      <van-field
        v-model="email"
        center
        clearable
        required
        label="短信验证码"
        placeholder="请输入短信验证码"
        name="email"
        :rules="[{ required: true, message: '请填写邮箱' }]"
      >
        <template #button>
          <van-button
            :disabled="showText ? false : true"
            size="small"
            type="primary"
            @click="sendEmail"
            >{{ showText ? "发送邮箱" : delayTime + "秒" }}</van-button
          >
        </template>
      </van-field>

      <!-- 验证码 -->
      <van-field
        v-model="code"
        required
        label="验证码"
        placeholder="请输验证码"
        name="code"
        :rules="[{ required: true, message: '请填写验证码' }]"
      />

      <!-- 提交 -->
      <div style="margin: 16px">
        <van-button round block type="info" native-type="submit"
          >提交</van-button
        >
      </div>
    </van-form>
  </div>
</template>

<script>
import { sendCode, register } from "@/apis/register.js";
export default {
  data() {
    return {
      name: "", //用户名
      pwd: "", //密码
      phone: "", //电话
      email: "", //邮箱
      sex: 1, //性别
      code: "", //短信验证码
      birth: "", //生日
      showPicker: false, //是否显示弹出层
      delayTime: 5, //倒计时
      showText: true, //是否显示发送邮箱文字
      timeHandler: "", //时间句柄
    };
  },
  methods: {
    async API_sendCode(email) {
      return await this.$http.request({
        url: sendCode,
        method: "POST",
        data: {
          email,
        },
      });
    },
    async API_register(email) {
      return await this.$http.request({
        url: register,
        method: "POST",
        data: {
          email,
        },
      });
    },
    goBack() {
      this.$router.go(-1);
    },
    onConfirm(value) {
      let date = this.$formTime(value);
      this.birth = date;
      this.showPicker = false;
    },
    async sendEmail() {
      this.showText = false;

      let res = await this.API_sendCode(this.email);
      console.log(res);

      this.timeHandler = setInterval(() => {
        if (this.delayTime <= 0) {
          this.delayTime = 5;
          this.showText = true;
          clearInterval(this.timeHandler);
        } else {
          this.delayTime--;
        }
      }, 1000);
    },
    onSubmit(val) {
      console.log(val);
    },
  },
};
</script>

<style></style>
